package com.ebpm.designer
{
	import com.ebpm.designer.item.Item;
	import com.ebpm.designer.line.Line;
	import com.ebpm.designer.line.Point;
	import com.ebpm.designer.line.XuLine;
	import com.ebpm.designer.mainLayout.variable.VariablePanel;
	import com.ebpm.designer.menu.Menu;
	import com.ebpm.designer.menu.SaveMenu;
	import com.ebpm.designer.pvm.UserTask;
	import com.ebpm.designer.xml.FileParser;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.system.Capabilities;
	
	import mx.collections.ArrayList;
	import mx.containers.Canvas;
	
	import spark.components.Application;
	import spark.components.Group;
	import spark.primitives.BitmapImage;
	
	/**
	 **********************************************************************
	 * @auth 王晓华
	 * @sice 2013-12
	 * 注意：
	 * 		1:如果有txtInput、处理文本等调用了 setFocus() 方法以后，记得将focus 设置回workpanel，否则键盘事件会失效
	 * 		2:application的app.stage.stageHeight可以拿到浏览器的有效高度
	 * @modifyDate 2016-09
	 * 		1:修复bug、优化代码、加入注释
	 * 		2:加入一些快捷键等额外的功能
	 * 		@注意：Flex的事件执行顺序，是由里往外执行。比如UserTask和WorkPanel都有mouseDown事件，先触发UserTask、再触发WorkPanel的mouseDown事件
	 **********************************************************************
	 **/
	public class WorkPanel extends Canvas
	{
		public var nodeContainers:ArrayList = new ArrayList();													//所有【NodeContainer】
		public var nodeContainer:NodeContainer;																		//选中【NodeContainer】
		public var lastNodeContaier:NodeContainer;																	//上个【NodeContainer】
		public var startEvent:UserTask;																							//【开始节点】
		public var endEvent:UserTask;																							//【结束节点】
		public var selectRect:SelectRect;																							//【选择框】
		public var selectLine:Line;																									//选中【线】
		
		/** 页面线 **/
		public var xuLine:XuLine;																										//虚线
		public var lineSourceItem:Item;																							//【源-画线点】
		public var lineTargetItem:Item;																							//【目标-画线点】
		/** 页面线 **/
		
		/** 参数面板 **/
		public var transferVariablePanel:VariablePanel;																//【线-参数面板】
		public var userTaskVariablePanel:VariablePanel;																//【用户任务-参数面板】
		public var startEventVariablePanel:VariablePanel;															//【开始事件-参数面板】
		public var endEventVariablePanel:VariablePanel;																//【结束事件-参数面板】
		public var serviceTaskVariablePanel:VariablePanel;															//【自动任务-参数面板】
		public var forkBeforeVariablePanel:VariablePanel;															//【分支前-参数面板】
		public var laneTaskVariablePanel:VariablePanel;																//【泳道任务-参数面板】
		public var variablePanels:ArrayList;																					//【所有参数面板 - 集合】
		/** 参数面板 **/
		
		public var menu:Menu;
		public function WorkPanel(app:spark.components.Application)
		{
			WF.app = app;
			WF.wp = this;
			this.width = Capabilities.screenResolutionX;
			this.height = WF.app.stage.stageHeight;
			this.horizontalScrollPolicy="auto";
			
//			initBackgroundImage();																										//设置背景图片
			this.setStyle("backgroundColor", "#F2F2F2");																	//设置背景颜色
			
			this.initVariablePanels();																										//初始化参数面板
			
			this.initListener();																													//初始化【WorkPanel监听器】
			
			this.xuLine = new XuLine();																									//添加【虚线】
			this.addElement(xuLine);
			this.selectRect = new SelectRect(this);																				//添加【选择框】
			this.addElement(this.selectRect);
			this.menu = new Menu("heng");																						//添加【菜单栏】
			this.addElement(menu);
			
		}
		
		//设置背景图片
		private function initBackgroundImage():void {
			var group:Group = new Group();
			var wfBackground:BitmapImage = new BitmapImage();
			wfBackground.source = WF.Icon_BackGround;
//			wfBackground.resizeMode = "repeat";
			wfBackground.x = 0;
			wfBackground.y = 0;
			wfBackground.width = this.width;
			wfBackground.height = this.height;
			group.addElement(wfBackground);
			this.addElement(group);
		}
		
		private function initVariablePanels():void {
			//初始化【参数面板】
			this.variablePanels = new ArrayList();																				
			transferVariablePanel = new VariablePanel(this, "transfer");
			this.addElement(transferVariablePanel);
			userTaskVariablePanel = new VariablePanel(this, "mission");
			this.addElement(userTaskVariablePanel);
			startEventVariablePanel = new VariablePanel(this, "startEvent");
			this.addElement(startEventVariablePanel);
			endEventVariablePanel = new VariablePanel(this, "endEvent");
			this.addElement(endEventVariablePanel);
			serviceTaskVariablePanel = new VariablePanel(this, "serviceTask");
			this.addElement(serviceTaskVariablePanel);
			forkBeforeVariablePanel = new VariablePanel(this, "forkBefore");
			this.addElement(forkBeforeVariablePanel);
			laneTaskVariablePanel = new VariablePanel(this, "laneTask");
			this.addElement(laneTaskVariablePanel);
			variablePanels.addItem(transferVariablePanel);
			variablePanels.addItem(userTaskVariablePanel);
			variablePanels.addItem(startEventVariablePanel);
			variablePanels.addItem(endEventVariablePanel);
			variablePanels.addItem(serviceTaskVariablePanel);
			variablePanels.addItem(forkBeforeVariablePanel);
			variablePanels.addItem(laneTaskVariablePanel);
		}
		
		private function initListener():void {
			//app大小变化监听，主要为了底部菜单栏变化
			WF.app.addEventListener(Event.RESIZE, _resize);
			this.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
			this.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMove);
			this.addEventListener(KeyboardEvent.KEY_DOWN, _keyDown);
			this.addEventListener(KeyboardEvent.KEY_UP, _keyUp);
		}
		
		private function _resize(event:Event):void
		{
			this.menu.y = WF.app.stage.stageHeight - (WF.MENU_ITEM_WIDTH + 10);
			this.height = WF.app.stage.stageHeight;
		}
		
		private function _mouseDown(event:MouseEvent):void {
			
			this.setFocus();
			
			WP.mouseDownX = this.mouseX;
			WP.mouseDownY = this.mouseY;
			WP.mouseDownStageX = this.contentMouseX;
			WP.mouseDownStageY = this.contentMouseY;
			
			if(WP.isAllowSelectNodeContainers) {
				WP.isDrawSelectRectEvent = true;
			}
			
			WP.isClickNodeContainer = this.isClickNodeContainer();
			
		}
		
		//关闭页面相关事件
		private function _mouseUp(event:MouseEvent):void	 {
			
			WP.mouseUpX = this.mouseX;
			WP.mouseUpY = this.mouseY;
			
			this.xuLine.graphics.clear();
			WP.isMovingMiddleLineDir = false;
			WP.isMovingFromLineDir = false;
			WP.isMovingEndLineDir = false;
			WP.isAllowSelectNodeContainers = true;
			WP.isDrawingLine = false;
			WP.isMovingSelectRectEvent = false;
			WP.isDrawSelectRectEvent = false;
			WP.isMovingLineTxtEvent = false;
			WP.isMovingNodeContainer = true;
			
			closeNodeContainersEvent();
			
			hideVariablePanelsByMouseUp();
			
			if(this.selectRect.nodeContainers.length <= 0) {
				this.selectRect.disVisible();
			}
		}
		
		private function _mouseMove(event:MouseEvent):void {
			
			// 设置移动坐标信息
			WP.mouseMoveX = this.mouseX;
			WP.mouseMoveY = this.mouseY;
			WP.mouseMoveStageX = this.contentMouseX;
			WP.mouseMoveStageY = this.contentMouseY;
			WP.mouseChangeX = WP.mouseMoveX - WP.mouseDownX;
			WP.mouseChangeY = WP.mouseMoveY - WP.mouseDownY;
			
			//移动参数面板
			this.movingVariablePanels();
			
			//移动选择框或者创建选择框
			if(WP.isMovingSelectRectEvent) {
				this.selectRect.movingNodeContainers();
			} else if(WP.isDrawSelectRectEvent) {
				this.selectRect.selectNodeContainers();
			}
			
			if(WP.isDrawingLine) {
				this.xuLine.drawing();
			} else {
				if(this.nodeContainer != null ) {
					//task移动事件
					this.nodeContainer.moving();
					//task推拽事件
					this.nodeContainer.resizing();
				}
			}
			
			if(WP.isMovingMiddleLineDir) {
				this.selectLine.resizeMiddleItem();
			}
			if(WP.isMovingFromLineDir) {
				this.selectLine.resizeFromItem();
			}
			if(WP.isMovingEndLineDir) {
				this.selectLine.resizeEndItem();
			}
			if(WP.isMovingLineTxtEvent) {
				this.selectLine.movingLabel(event);
			}
			
		}
		
		private function _keyDown(event:KeyboardEvent):void {
			
			//1、不为页面文本编辑事件时
			if(!WP.isEditing) 
			{
				
				if(event.keyCode == 38) { 																								
					
					//上
					if(WP.isMovingSelectRectByKeyboard) {
						this.selectRect.movingNodeContainersByKeyboard(0, -1);
					} else {
						this.nodeContainer.movingByKeyBoard(0, -1);
					}
					
				} else if(event.keyCode == 40) {
					
					//下
					if(WP.isMovingSelectRectByKeyboard) {
						this.selectRect.movingNodeContainersByKeyboard(0, 1);
					} else {
						this.nodeContainer.movingByKeyBoard(0, 1);
					}
					
				} else if(event.keyCode == 37) {
					
					//左
					if(!WP.isCtrlDown) {
						if(WP.isMovingSelectRectByKeyboard) {
							this.selectRect.movingNodeContainersByKeyboard(-1, 0);
						} else {
							this.nodeContainer.movingByKeyBoard(-1, 0);
						}
					} else {
						this.nodeContainer.width = this.nodeContainer.width - 1;
						this.nodeContainer.resizingChildren();
					}
					
				} else if(event.keyCode == 39) {
					
					//右
					if(!WP.isCtrlDown) {
						if(WP.isMovingSelectRectByKeyboard) {
							this.selectRect.movingNodeContainersByKeyboard(1, 0);
						} else {
							this.nodeContainer.movingByKeyBoard(1, 0);
						}
					} else {
						this.nodeContainer.width = this.nodeContainer.width + 1;
						this.nodeContainer.resizingChildren();
					}
					
				} else if(event.keyCode == 46) {
					
					//delete
					if(this.selectLine != null) {
						this.selectLine.deleteMyself();
						this.selectLine = null;
					} else if(this.selectRect.nodeContainers != null && this.selectRect.nodeContainers.length != 0) {
						this.selectRect.deleteNodeContainers();
					} else {
						if(this.nodeContainer != null) {
							this.nodeContainer.deleteMySelf();
						}
					}
					
				} 
				
			}
			
			// Ctrl
			if(event.keyCode == 17) { 																						
				WP.isCtrlDown = true;
			} else if(event.keyCode == 83) {																							
				// S
				if(WP.isCtrlDown) {
					SaveMenu.saveFile();
				}
			} else if(event.keyCode == 65) {																					
				//A
				if(WP.isCtrlDown) {
					var p:Point = this.getMaxContainerPoint();
					var min:Point = this.getMinContainerPoint();
					this.selectRect.select(min.x , min.y, p.x + 10, p.y + 10, false);
				}
				WP.isCtrlDown = false;
			} else if(event.keyCode == 79) {
				if(WP.isCtrlDown) {
					FileParser.loadDefineFile2WorkPanel();
				}
			} else if(event.keyCode == 90) {
				if(WP.isCtrlDown) {
					new NodeContainer(this.mouseX - WF.MIN_NODE_W/2, this.mouseY - WF.MIN_NODE_H/2, WF.wp, "UserTask");
					this.setFocus();
				}
			}
		}
		
		private function _keyUp(event:KeyboardEvent):void {
			if(event.keyCode == 17) {
				WP.isCtrlDown = false;
			} else if(event.keyCode == 46) {
				WP.isEditing = false;
			}
		}
		
		//移动参数面板
		private function movingVariablePanels():void {
			
			for(var i:Number=0; i<this.variablePanels.length; i++) {
				var temp:VariablePanel = this.variablePanels.getItemAt(i) as VariablePanel;
				temp.moving();
			}
			
		}
		
		private function closeNodeContainersEvent():void {
			
			for(var i:int=0; i<this.nodeContainers.length; i++) {
				var c:NodeContainer = this.nodeContainers.getItemAt(i) as NodeContainer;
				c.isResizing = false;
				c.isMoving = false;
				if(!WP.isClickNodeContainer) {
					c.mission.closeEditing();
				}
				c.removeDirs();
//				c.hideItems();
				c.setLinesDefaultColor();
			}
			
		}
		
		private function hideVariablePanelsByMouseUp():void {
			
			var isDisplay:Boolean = this.isClickNodeContainer();
			
			//1、判断是否点击【VariablePanel】本身
			if(this.mouseX > VariablePanel.publicX && this.mouseX < (VariablePanel.publicX + 248) 
				&& this.mouseY > VariablePanel.publicY && VariablePanel.publicY < (this.mouseY + 480)) {
				isDisplay = true;
			}
			
			//2、点击【空面板】则隐藏【参数面板】
			if(!isDisplay) {
				this.hideVariablePanels();
			}
			
		}
		
		public function getMaxContainerPoint():Point {
			var maxX:Number = 0;
			var maxY:Number = 0;
			for(var i:Number=0; i<WF.wp.nodeContainers.length; i++) {
				var n:NodeContainer = WF.wp.nodeContainers.getItemAt(i) as NodeContainer;
				var x:Number = n.x + n.width;
				var y:Number = n.y + n.height;
				if(x > maxX) maxX = x;
				if(y > maxY) maxY = y;
			}
			return new Point(maxX, maxY);
		}
		
		public function getMinContainerPoint():Point {
			var minX:Number = 10000;
			var minY:Number = 10000;
			for(var i:Number=0; i<WF.wp.nodeContainers.length; i++) {
				var n:NodeContainer = WF.wp.nodeContainers.getItemAt(i) as NodeContainer;
				var x:Number = n.x;
				var y:Number = n.y;
				if(x < minX) minX = x;
				if(y < minY) minY = y;
			}
			return new Point(minX, minY);
		}
		
		//删除所有【连线点】
		public function hideNodeContainerItems():void {
			for(var i:int=0; i<this.nodeContainers.length; i++) {
				var tt:NodeContainer = this.nodeContainers.getItemAt(i) as NodeContainer;
				tt.hideItems();
			}
		}
		
		public function hideVariablePanels():void {
			
			for(var i:Number=0; i<this.variablePanels.length; i++) {
				var temp:VariablePanel = this.variablePanels.getItemAt(i) as VariablePanel;
				if(this.contains(temp)) {
					this.removeElement(temp);
				}
			}
			
		}
		
		//判断是否点击了【NodeContaiter】
		public function isClickNodeContainer():Boolean {
			
			for(var i:int=0; i<this.nodeContainers.length; i++) {
				var c:NodeContainer = this.nodeContainers.getItemAt(i) as NodeContainer;
				if(c.isCover(this.mouseX, this.mouseY)) {
					return true;	
				}
			}
			
			return false;
			
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}